package cl.orangepeople.whyem.portal.negotiations.converter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.apache.commons.lang3.StringUtils;

public class AmountConverter implements Converter, Serializable {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
	return new BigDecimal("259990");
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
	if (value instanceof String && StringUtils.isBlank((String) value)) {
	    return StringUtils.EMPTY;
	} else {
	    BigDecimal amount = new BigDecimal(String.valueOf(value));
	    return formatAmount(amount);
	}
    }

    private String formatAmount(Object trxAmount) {
	Locale locale = new Locale("es", "CL");
	NumberFormat numberFormatter = NumberFormat.getCurrencyInstance(locale);
	String trxAmountFormatted = numberFormatter.format(trxAmount);
	trxAmountFormatted = StringUtils.remove(trxAmountFormatted, "Ch");
	
	return trxAmountFormatted;
    }
}